'use strict'

angular.module('webAppApp')
  .controller 'MainCtrl', ($scope, $http) ->
    
    $http.get("/posts").success (data) ->
      $scope.posts = data["posts"]
      console.log $scope.posts

    $scope.savePost = (data, id, index) ->
      if id
        return $http.put("/posts/#{id}", data)
      else
        return $http.post("/posts", data).success (data)->
          $scope.posts[index].id = data.id


    $scope.addPost = ->
      $scope.posts.push
        "id": null
        "title": null
        "content": null

    $scope.removePost = (index) ->
      $http.delete("/posts/#{$scope.posts[index].id}").success ->
        $scope.posts.splice(index, 1)
